import * as ChargeConstant from '../constants/ChargeConstant';

import {get, post} from '../../../utils/url';
import {chargeRecordHistoryAPI,
payWithOnlineQuickAPI,
companyQuickPaymentsAPI,
companyBankPaymentsAPI,
saveCompanyThirdPaymentAPI,
saveCompanyBankPaymentAPI,
chargePaymentItemsAPI,
saveScanPaymentAPI,
onlineQuickPaymentsAPI} from '../utils/API';
import {format} from '../../../utils/datetime';

export function loadChargeRecord(beginTime, endTime, pageNo = 1, pageSize = 10,cb = () => {} ) {
  return (dispatch, getState) => {
    let userName = getState().userModule.user.get('auth').get('userName');
    post(chargeRecordHistoryAPI(), {
      userName,
      beginTime,
      endTime,
      pageNo,
      pageSize
    })
    .then(res => res.json())
    .then(json => {
      cb(json);
      dispatch({
        type: ChargeConstant.REQUEST_CHARGE_RECORD_HISTORY,
        pageNo,
        data: json
      });
    });
  };
}

export function payWithOnlineQuick(money, payId, payValue, payType, cb = () => {}) {
  return (dispatch, getState) => {
    let userName = getState().userModule.user.get('auth').get('userName');
    post(payWithOnlineQuickAPI(), {
      userName,
      money,
      payId,
      payValue,
      payType
    })
    .then( res => res.json())
    .then(data => {
      cb(data);
    });
  };
}

export function loadOnlineQuickPayments() {
  return (dispatch, getState) => {
    let userName = getState().userModule.user.get('auth').get('userName');
    post(onlineQuickPaymentsAPI(), {
      userName
    })
    .then(res => res.json())
    .then(data => {
      dispatch({
        type: ChargeConstant.REQUEST_ONLINE_QUICK_PAYMENTS,
        data
      });
    });
  };
}

export function loadCompanyQuickPayments() {
  return (dispatch, getState) => {
    let userName = getState().userModule.user.get('auth').get('userName');
    post(companyQuickPaymentsAPI(), {
      userName
    })
    .then(res => res.json())
    .then(data => {
      dispatch({
        type: ChargeConstant.REQUEST_COMPANY_QUICK_PAYMENTS,
        data
      });
    });
  };
}

export function loadCompanyBankPayments() {
  return (dispatch, getState) => {
    let userName = getState().userModule.user.get('auth').get('userName');
    post(companyBankPaymentsAPI(), {
      userName
    })
    .then(res => res.json())
    .then(data => {
      dispatch({
        type: ChargeConstant.REQUEST_COMPANY_BANK_PAYMENTS,
        data
      });
    });
  };
}

// 获取充值支付列表 - 包括 在线支付 扫码支付 银行到账
export function loadChargePaymentItems() {
  return (dispatch, getState) => {
    let userName = getState().userModule.user.get('auth').get('userName');
    post(chargePaymentItemsAPI(),  {userName})
    .then(res => res.json())
    .then(data => {
      dispatch({
        type: ChargeConstant.REQUEST_CHARGE_PAYMENT_ITEMS,
        data
      });
    });
  };
}

export function saveCompanyThirdPayment(money, payType, payNo, time, hkUserName, cb = () => {}) {
  return (dispatch, getState) => {
    let userName = getState().userModule.user.get('auth').get('userName');
    post(saveCompanyThirdPaymentAPI(), {
      userName,
      money,
      payType,
      payNo,
      time,
      hkUserName
    })
    .then(res => res.json())
    .then(data => {
      cb(data);
    });
  };
}

export function saveScanPayment(money, payType, payNo, hkUserBank, cb = () => {}) {
  return (dispatch, getState) => {
    let userName = getState().userModule.user.get('auth').get('userName');
    post(saveScanPaymentAPI(), {
      userName,
      money,
      payType,
      payNo,
      hkUserBank
    })
    .then(res => res.json())
    .then(data => {
      cb(data);
    });
  };
}

export function saveCompanyBankPayment(money, payNo, time, payType, hkUserName, companyBank = '',  hkUserBank = '', cb = () => {} ) {
  return (dispatch, getState) => {
    let userName = getState().userModule.user.get('auth').get('userName');
    let params = {
      userName,
      money,
      payNo,
      time,
      payType,
      hkUserName
    };

    if (companyBank) {
      params['companyBank'] = companyBank;
    }
    if (hkUserBank) {
      params['hkUserBank'] = hkUserBank;
    }
    post(saveCompanyBankPaymentAPI(), params)
    .then(res => res.json())
    .then(data => {
      cb(data);
    });
  };
}